//
//  FDNavigationViewController.m
//  FD
//
//  Created by xiaofang wang on 2018/11/16.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "FDNavigationViewController.h"

@interface FDNavigationViewController ()<UIGestureRecognizerDelegate>

@end

@implementation FDNavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    self.interactivePopGestureRecognizer.delegate=self;
}


//重写push方法,拦截所有push进来的子控制器

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {

    //如果不是第一个push进来的控制器
    if (self.childViewControllers.count > 0) {
//替换左上角返回按钮
        UIButton *backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [backBtn setImage:[UIImage imageNamed:@"返回箭头.png"] forState:(UIControlStateNormal)];
        [backBtn addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];

        //iOS 10的bug,不给frame真机上不显示 其他系统ok
        backBtn.frame = CGRectMake(0, 0, 40, 30);
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0); //1-3

        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
        
        //隐藏底部tabbar工具条
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    //所有搞定完成之后调用父类方法
    [super pushViewController:viewController animated:animated];
}

- (void)back {
    [self popViewControllerAnimated:YES];
}


#pragma mark - <UIGestureRecognizerDelegate>
/**
 *  手势识别器对象会调用这个代理方法来决定手势是否有效
 *
 *  @return YES : 手势有效, NO : 手势无效
 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    // 手势何时有效 : 当导航控制器的子控制器个数 > 1就有效
    return self.childViewControllers.count > 1;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
